package cn.cdeden.knowledge.controller.online.factory.jurisdiction;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.knowledge.domain.EnterpriseFilesTree;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.enums.TreeEnum;
import cn.cdeden.knowledge.enums.TreeTypeEnum;
import cn.cdeden.knowledge.service.EnterpriseFilesTreeService;
import com.alibaba.fastjson.JSONObject;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @author username
 * @date 2024/12/11 19:35
 * @describe:
 **/
@RequiredArgsConstructor
@Component("enterprise")
public class JurisdictionEnterprise implements Jurisdiction{

    private final EnterpriseFilesTreeService enterpriseFilesTreeService;
    @Override
    public JurisdictionResponse getFileJurisdictionDownloadFile(String id, JSONObject jsonObject) throws BadRequestException {
        EnterpriseFilesTree byId = enterpriseFilesTreeService.getById(id);
        if (ObjectUtils.isEmpty(byId) && !StringUtils.equalsIgnoreCase(TreeTypeEnum.FILE.getType(),byId.getType())){
            throw new BadRequestException("错误的 文件编码 或 不是文件");
        }
        return JurisdictionResponse.success(byId.getStorageId(),byId.getStorageType(),byId.getRealName(),byId.getSuffix());
    }

    @Override
    public JurisdictionResponse getFileDate(String id, JSONObject jsonObject, String userId) throws BadRequestException {
        EnterpriseFilesTree byId = enterpriseFilesTreeService.getById(id);
        if (ObjectUtils.isEmpty(byId) || !StringUtils.equalsIgnoreCase(TreeTypeEnum.FILE.getType(),byId.getType())){
            throw new BadRequestException("错误的 文件编码 或 不是文件");
        }
        FilesTree tree = MapstructUtils.convert(byId, FilesTree.class);
        tree.setLibraryType(TreeEnum.PERSONAL.getType());

        JurisdictionResponse success = JurisdictionResponse.success(tree);
//        success.setWatermarkTxt("#{[nick_name]}_#{[dept_name]}_#{[phone04]}"); //水印 文件
        return success;
    }
}
